#!/bin/bash
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2016-2019 GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

if [ $GEM_SET_DEBUG ]; then
    set -x
fi
set -e

if [ $(id -u) -gt 0 ]; then
    echo -e "!! This script requires sudo.\nPlease run 'sudo $0'." >&2
    exit 1
fi

check_dep() {
    for i in $*; do
        command -v $i &> /dev/null || {
            echo -e "!! $i not found. Aborting." >&2
            exit 1
        }
    done
}

startstop() {
    local ACTION=$1
    local SERVICE=${2:-openquake-dbserver}

    if [ -f /lib/systemd/system/${SERVICE}.service ]; then
        systemctl $ACTION ${SERVICE}.service
    else
        echo -e "!! Service $SERVICE not found. Aborting." >&2
        exit 1
    fi
}

getdbpath() {
    local OQ_CFG='/etc/openquake/openquake.cfg'
    if [ -f $OQ_CFG ]; then
        grep 'file = ' $OQ_CFG | tr -d ' ' | cut -d '=' -f 2
    else
        echo -e "!! Configuration file not found. Aborting." >&2
        exit 1
    fi
}

help() {
    cat <<HSD
The command line arguments are as follows:

    -s, --skip-new       The new database will not be created
    -y, --yes            Don't pause for user input, assume yes on all questions
    -h, --help           This help
HSD
    exit 0
}

BOLD=`tput bold`
NORMAL=`tput sgr0`

check_dep oq find

while (( "$#" )); do
    case "$1" in
        -y|--yes)
            force=y; shift;;
        -s|--skip-new)
            skipnew=y; shift;;
        -h|--help)
            help; shift;;
    esac
done

echo ''
echo "!! ${BOLD}This script is going to remove the OpenQuake Engine data${NORMAL}"
echo '!!'
echo -e "!! \t*  Make sure no calculations are running"
echo -e "!! \t*  The OpenQuake Engine datastores located in /home/[user]/oqdata will be removed"
echo -e "!! \t*  The OpenQuake Engine database will be dropped"
echo -e "!! \t   All the old data contained will be lost"
if [ -z $skipnew ]; then
    echo -e "!! \t*  A new OpenQuake Engine database will be created"
fi
echo '!!'
if [ $force ]; then
    for i in {3..1}; do
        echo -ne "\r!! Press CTRL-C to cancel the operation within ${i} seconds"
        sleep 1
    done
    answer="y"
else
    echo -n "!! Do you want to proceed? (y/n)"
    read -n 1 -s answer
fi
echo ''
echo ''

if [[ "$answer" == "y" ]]; then
    echo " * OpenQuake Engine DbServer stopped"
    startstop stop
    rm $(getdbpath)
    echo " * Database dropped"
    find /home -maxdepth 2 -type d -name "oqdata" -exec rm -Rf '{}' \; || true
    if [ -z $skipnew ]; then
        startstop start
        echo " * Migrating schema"
        cd /usr/share/openquake/engine || true
        python3 -m openquake.server.manage migrate
        echo " * Database created"
    fi
    echo ''
    echo "!!"
    echo "!! DONE"
    echo "!!"
    echo ''
fi
